//
//  LZFTopicItem.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/7/2.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFTopicItem.h"
#import "NSString+LZFCalTextSize.h"
#import "LZFCommentItem.h"
#import "LZFUserItem.h"


@implementation LZFTopicItem

-(CGFloat)rowHeight
{
    //算过的话就直接返回
    if (_rowHeight) return _rowHeight;
    
    //没算过的话才开始算rowHeight
    
    
    
    //统一的间距
    CGFloat margin =10;
    //头像高度
    CGFloat iconHeight=60;
    
    //文本高度
    CGFloat textHeight=0;
    
    //中间xib高度
    CGFloat middleHeigth=0;
    
    //评论高度
    CGFloat commentHeight=0;
    
    //工具条高度
    CGFloat toolHeigth=40;
    
    //算文本高度
    CGSize textMaxSize=CGSizeMake(screenW-20, MAXFLOAT);
    NSMutableDictionary *dic=[NSMutableDictionary new];
    dic[NSFontAttributeName]=[UIFont systemFontOfSize:17];
    CGRect maxRect =[NSString calTextSize:self.text withMaxSize:textMaxSize withAttrDic:dic];
    textHeight=maxRect.size.height;
    
    
    
    //中间的frame
    //中间是非文字才这样算
    if (self.type!=LZFTopicTypeWord) {
        CGFloat middleX=margin;
        CGFloat middleY=iconHeight+textHeight+3*margin;
        CGFloat middleW=textMaxSize.width;
        CGFloat middlwH=self.height*middleW/self.width;
        
        //根据转换后的高度和screenH比较,判断是否是大图片,设置对应的属性
        if (middlwH>screenH) {
            self.bigPicture=YES;
            middlwH=screenH*0.3;
        }
        
        self.middleFrame=CGRectMake(middleX,middleY,middleW,middlwH);
        middleHeigth=middlwH;
        
    }
    
    //评论高度
    CGFloat commentTitleHeight=20;
    
    if (self.topComment==nil) {
    //没有评论
        commentHeight=-10;
    }
    else
    {
        
        //有评论
//        NSDictionary *cmt=self.top_cmt[0];
//        NSString *content=cmt[@"content"];
//        NSString *username=cmt[@"user"][@"username"];
        NSString *content=self.topComment.content;
        NSString *username=self.topComment.user.username;
        if (content.length==0) {
          //语音评论
            NSString *a=[NSString stringWithFormat:@"语音评论:%@",username];
            commentHeight=commentTitleHeight+[NSString calTextSize:a withMaxSize:textMaxSize withAttrDic:dic].size.height;
        }
        else
        {
          //非语音评论
            NSString *b=[NSString stringWithFormat:@"%@:%@",username,content];
            commentHeight=commentTitleHeight+[NSString calTextSize:b withMaxSize:textMaxSize withAttrDic:dic].size.height;
        }
    }
    

    _rowHeight=6*margin+iconHeight+textHeight+middleHeigth+commentHeight+toolHeigth;
    
    return _rowHeight;
}


@end
